# -*- coding: utf-8 -*-

# Copyright: (c) 2007 by Jarek Zgoda, <jzgoda@o2.pl>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Steet, Fifth Floor, Boston, MA  02111-1307, USA.

"""
LastFM Service Client plugin for Rhythmbox (http://www.rhythmbox.org/).
"""

__revision__ = '$Id: __init__.py 39 2007-05-06 11:25:04Z jarek.zgoda $'

import gtk, gtk.glade

import rb


class LastFMClientWindow(gtk.Window):

    def __init__(self, shell, widget_tree):
        gtk.Window.__init__(self)
        self.set_border_width(12)
        self.set_transient_for(shell.get_property('window'))
        self.set_title(_('Last.FM Client Tools'))
        self.widget_tree = widget_tree
        self.widget_tree.signal_autoconnect(self)
        vbox = gtk.VBox(spacing=12)
        self.add(vbox)
        self.notebook = self.widget_tree.get_widget('nbk_main')
        vbox.pack_start(self.notebook)
        btn_close = gtk.Button(stock=gtk.STOCK_CLOSE)
        btn_close.connect('clicked', lambda w: self.hide_all())
        bbox = gtk.HButtonBox()
        bbox.set_layout(gtk.BUTTONBOX_END)
        bbox.pack_start(btn_close)
        vbox.pack_start(bbox, expand=False)
        self.set_default_size(400, 300)
        self._update_widgets(shell.get_property('db'))

    def _update_widgets(self, db):
        pass


class LastFMClientPlugin(rb.Plugin):

    def __init__(self):
        rb.Plugin.__init__(self)
        self.main_window = None

    def activate(self, shell):
        self.main_action = gtk.Action('LastFMClientMain', _('Last.FM client'),
            _('Last.FM Services Client'), 'rb-lastfm-client')
        self.main_action.connect('activate', self.show_client_window, shell)
        self.popup_action = gtk.Action('BrowserSourcePopupLastFMClient',
            _('Last.FM client'), _('Last.FM Services Client'), 'rb-lastfm-client')
        self.tag_action = gtk.Action('LastFMClientTag', _('Tags'),
            _('Tag item in last.fm'), None)
        self.recommend_action = gtk.Action('LastFMClientRecommend', _('Recommendations'),
            _('Recommend item in last.fm'), None)
        self.love_ban_action = gtk.Action('LastFMClientLoveBan', _('Love or Ban'),
            _('Add item to loved/banned lists'), None)
        self.action_group = gtk.ActionGroup('LastFMClientPluginActions')
        self.action_group.add_action(self.main_action)
        self.action_group.add_action(self.popup_action)
        self.action_group.add_action(self.tag_action)
        self.action_group.add_action(self.recommend_action)
        self.action_group.add_action(self.love_ban_action)
        uim = shell.get_ui_manager()
        uim.insert_action_group(self.action_group, 0)
        self.ui_id = uim.add_ui_from_file(self.find_file('lastfmcli.ui.xml'))
        player = shell.get_player()
        self.psc_id = player.connect('playing-song-changed',
            self.playing_song_changed)
        self.playing_song_changed(player, player.get_playing_entry)
        uim.ensure_update()

    def deactivate(self, shell):
        # following Doc, some cleanup is necessary here
        # seems bit unpythonic, but we are guests of RB, so be polite
        uim = shell.get_ui_manager()
        uim.remove_ui(self.ui_id)
        uim.remove_action_group(self.action_group)
        self.action_group = None
        self.main_action = None
        self.popup_action = None
        self.tag_action = None
        self.recommend_action = None
        self.love_ban_action = None
        self.main_window = None
        player = shell.get_player()
        player.disconnect(self.psc_id)

    def playing_song_changed(self, player, entry):
        self.main_action.set_sensitive(entry is not None)
        self.popup_action.set_sensitive(entry is not None)

    def show_client_window(self, action, shell):
        player = shell.get_player()
        widget_tree = gtk.glade.XML(self.find_file('lastfmcli.glade'),
            root='nbk_main', domain='lastfmcli')
        main_window = LastFMClientWindow(shell, widget_tree)
        main_window.show_all()
